package com.amigo.online.provider.adv.service.common;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.amigo.online.provider.adv.common.RwRedisService;
import com.amigo.online.provider.adv.util.WeightRandom;

public abstract class AbstractAdvApplyService {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    protected RwRedisService rwRedisService;

    protected Set<Integer> getRandomAdvIds(Set<Integer> areaAdvSet, List<Integer> remainingList, int number) {
        // 根据权重算法，获取对应数广告
        List<Pair<Integer, Integer>> list = new ArrayList<Pair<Integer, Integer>>();
        int i = 0;
        for (Integer temp : areaAdvSet) {
            if (remainingList.get(i) != null) {
                Pair<Integer, Integer> tempPair = new ImmutablePair<>(temp, remainingList.get(i));
                list.add(tempPair);
            }
            i++;
        }

        WeightRandom<Integer, Integer> w = new WeightRandom<Integer, Integer>(list);
        Set<Integer> advSet = new HashSet<>(4);
        int tryTimes = 0;
        while (advSet.size() < number) {
            advSet.add(w.random());
            tryTimes++;
            if (tryTimes == 3 * number) {
                break;
            }
        }
        return advSet;
    }

    public static void main(String[] args) {
        Set<Long> set = new HashSet<>();
        for (long i = 1; i < 1000; i++) {
            System.out.print(i ^ (i >>> 32));
            System.out.print(",");

        }
        System.out.println("-------------------");
        for (Long temp : set) {
            System.out.print(temp);
            System.out.print(",");
        }
        Iterator<Long> it = set.iterator();
        System.out.println("-------------------");
        while (it.hasNext()) {

            System.out.print(it.next());
            System.out.print(",");
        }
    }
}
